package com.woniuxy.cinemaServer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.cinemaServer.dto.cinema.CinemaInfoDto;
import com.woniuxy.cinemaServer.exception.cinema.CinemaException;
import com.woniuxy.cinemaServer.exception.cinema.CinemaExceptionCode;
import com.woniuxy.cinemaServer.model.CinemaInfo;
import com.woniuxy.cinemaServer.dao.CinemaInfoMapper;
import com.woniuxy.cinemaServer.model.SeatInfo;
import com.woniuxy.cinemaServer.param.CinemaChangeParam;
import com.woniuxy.cinemaServer.param.cinema.CinemaAddParam;
import com.woniuxy.cinemaServer.param.cinema.CinemaLoadParam;
import com.woniuxy.cinemaServer.service.CinemaInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.cinemaServer.service.SeatInfoService;
import com.woniuxy.cinemaServer.util.StateUtil;
import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年03月20日
 */
@Service
public class CinemaInfoServiceImpl extends ServiceImpl<CinemaInfoMapper, CinemaInfo> implements CinemaInfoService {
    @Autowired
    private CinemaInfoMapper cinemaInfoMapper;
    @Autowired
    private SeatInfoService seatInfoService;

    @Override
    @Transactional
    public void add(CinemaAddParam param) {
        CinemaInfo cinemaInfo = BeanUtil.toBean(param, CinemaInfo.class);
        //用名字判断
        QueryWrapper<CinemaInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cinema_name",cinemaInfo.getCinemaName());
        CinemaInfo cinemaInfo1 = cinemaInfoMapper.selectOne(queryWrapper);
        if (cinemaInfo1!=null) throw new CinemaException(CinemaExceptionCode.CINEMA_HAD_EXIST);
        cinemaInfo.setCinemaStatus(StateUtil.CINEMA_STATE_NORMAL);
        cinemaInfo.setCreateDate(LocalDateTime.now());
        cinemaInfoMapper.insert(cinemaInfo);
        ArrayList<SeatInfo> seatInfos = new ArrayList<>();
        for (int i=0;i<20;i++){
            for (int j=0;j<(cinemaInfo.getCinemaMax()%20==0?cinemaInfo.getCinemaMax()/20:cinemaInfo.getCinemaMax()+1);j++){
                SeatInfo seatInfo = new SeatInfo();
                seatInfo.setSeatCol(i+1);
                seatInfo.setSeatRow(j+1);
                seatInfo.setSeatStatus(1);
                seatInfo.setCinemaId(cinemaInfo.getCinemaId());
                seatInfos.add(seatInfo);
            }
        }
        seatInfoService.saveBatch(seatInfos);
    }

    @Override
    public void del(Integer id) {
        //判断
        CinemaInfo cinemaInfo = cinemaInfoMapper.selectById(id);
        if (cinemaInfo==null) throw new CinemaException(CinemaExceptionCode.CINEMA_NOT_EXIST);
        if (cinemaInfo.getCinemaStatus()!=StateUtil.CINEMA_STATE_NORMAL) throw new CinemaException(CinemaExceptionCode.CINEMA_STATE_ERROR);
        cinemaInfo.setCinemaStatus(StateUtil.CINEMA_STATE_DISABLED);
        cinemaInfoMapper.updateById(cinemaInfo);
    }

    @Override
    public void change(CinemaChangeParam param) {
        CinemaInfo cinemaInfo = BeanUtil.toBean(param, CinemaInfo.class);
        //判断
        CinemaInfo cinemaInfo1 = cinemaInfoMapper.selectById(param.getCinemaId());
        if (cinemaInfo==null) throw new CinemaException(CinemaExceptionCode.CINEMA_NOT_EXIST);
        cinemaInfo.setUpdateDate(LocalDateTime.now());
        cinemaInfoMapper.updateById(cinemaInfo);
    }

    @Override
    public PageInfo<CinemaInfoDto> load(CinemaLoadParam param) {
        QueryWrapper<CinemaInfo> w = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(param.getName())) w.eq("cinema_name",param.getName());
        PageHelper.startPage(param.getPageNum()==null?1:param.getPageNum(),param.getPageSize()==null?3:param.getPageSize());
        List<CinemaInfo> cinemaInfos = cinemaInfoMapper.selectList(w);
        PageInfo<CinemaInfo> cinemaInfoPageInfo = new PageInfo<>(cinemaInfos);
        ArrayList<CinemaInfoDto> cinemaInfoDtos = new ArrayList<>();
        for (CinemaInfo cinemaInfo : cinemaInfos) {
            cinemaInfoDtos.add(BeanUtil.toBean(cinemaInfo,CinemaInfoDto.class));
        }
        PageInfo<CinemaInfoDto> pageInfo = new PageInfo<>(cinemaInfoDtos);
        pageInfo.setPageNum(cinemaInfoPageInfo.getPageNum());
        pageInfo.setPageSize(cinemaInfoPageInfo.getPageSize());
        pageInfo.setPages(cinemaInfoPageInfo.getPages());
        pageInfo.setTotal(cinemaInfoPageInfo.getTotal());
        return pageInfo;
    }
}
